package qgs.zisu.javaconcurrency.javaconcurrency.chapter12_ThreadGroup;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/20 14:51
 * @Version 1.0
 */
public class ThreadGroupCreate {
    public static void main(String[] args) {
        //use the name
        ThreadGroup tg1 = new ThreadGroup("TG1");
        Thread t1 = new Thread(tg1,"t1"){
            public boolean flag = true;
            @Override
            public void run(){
                while (flag){
                    try {
                        System.out.println("getName(in)"+getThreadGroup().getName());
                        System.out.println("getParent(in)"+getThreadGroup().getParent());
                        System.out.println("activeCount(in)"+getThreadGroup().activeCount());
                        Thread.sleep(1_000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    flag = false;
                }
            }
        };
        t1.start();
        System.out.println("getName(out)"+t1.getThreadGroup().getName());
        //use the parent and groupname
        ThreadGroup tg2 = new ThreadGroup(tg1,"TG2");
        System.out.println("tg2getParent(out):"+tg2.getParent());
        System.out.println("tg2getName(out):"+tg2.getName());


        System.out.println("当前线程名"+Thread.currentThread().getName());
        System.out.println("当前线程，所属线程组"+Thread.currentThread().getThreadGroup());
    }
}
